<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\ResourceReadingList;
use App\Models\ResourceReadingListBook;
use App\Validate\ResourceReadingListValidate;
use Illuminate\Support\Facades\Log;

/**
 * 推荐书单列表
 */
class ResourceReadingListController extends Controller
{
    public $model = null;
    public $bookModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceReadingList();
        $this->bookModel = new ResourceReadingListBook();
        $this->validate = new ResourceReadingListValidate();
    }


    /**
     * 获取推荐书单列表
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     */
    public function getReadingList()
    {
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;

        $res = $this->model->lists(['is_play' => 1, 'limit' => $limit]);
        if ($res['data']) {
            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }



    /**
     * 获取书单详情
     * @param reading_id  书单id
     * @param big_type_id 大类id
     */
    public function getReadingDetail()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('wx_get_reading_detail')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $reading_id = $this->request->reading_id;
        $big_type_id = $this->request->big_type_id;
        $res = $this->model->detail($reading_id);
        if ($res) {
            $res['number'] =  $this->bookModel->getResourceReadingListBookCount($reading_id , $big_type_id);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }
    /**
     * 根据书单id获取书籍列表
     * @param reading_id  书单id
     * @param big_type_id 大类id
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     */
    public function getReadingBookList()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('wx_get_reading_book_list')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $reading_id = $this->request->reading_id;
        $big_type_id = $this->request->big_type_id;
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;

        //以资源商显示数据
        $res = $this->bookModel->lists(['reading_id' => $reading_id, 'big_type_id' => $big_type_id, 'limit' => $limit]);
        if (!empty($res['data'])) {
            $res = $this->disPageData($res);
            foreach ($res['data'] as $key => $val) {
                $res['data'][$key]['browse_num'] = !empty($val['browse_num']) ? format_number($val['browse_num']) : null; //位数转换
            }
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }
}
